package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.GoodsCategoryService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*

@RequestMapping("goodsCategories")
@RestController
class GoodsCategoryController(
    private val goodsCategoryService: GoodsCategoryService
) {

    data class QueryGoodsCategoriesRequest(
        val name: String?,
    )

    @HasAuthority(Authority.货品_分类_查询)
    @GetMapping(params = ["page"])
    fun queryGoodsCategories(pageable: Pageable, queryGoodsCategoryRequest: QueryGoodsCategoriesRequest): Page<QueryGoodsCategoriesResponse> {
        return this.goodsCategoryService.queryGoodsCategories(pageable,queryGoodsCategoryRequest)
    }

    data class QueryGoodsCategoriesResponse(
        val name:String,
        val id:Int,
        val goodsAmount:Int
    )

    data class UpdateGoodsCategoryRequest(
        val name: String,
    )

    @HasAuthority(Authority.货品_分类_修改)
    @PutMapping("{id}")
    fun updateGoodsCategory(@PathVariable id:Int,@RequestBody updateGoodsCategoryRequest: UpdateGoodsCategoryRequest){
        this.goodsCategoryService.updateGoodsCategory(id,updateGoodsCategoryRequest)
    }

    @HasAuthority(Authority.货品_分类_新增)
    @PostMapping
    fun createGoodsCategory(@RequestBody updateGoodsCategoryRequest: UpdateGoodsCategoryRequest){
        this.goodsCategoryService.createGoodsCategory(updateGoodsCategoryRequest)
    }

    @DeleteMapping("{id}")
    fun deleteGoodsCategory(@PathVariable id:Int){
        this.goodsCategoryService.deleteGoodsCategory(id)
    }

    @GetMapping
    fun getAllGoodsCategories():List<IntIdAndName>{
        return this.goodsCategoryService.getAllGoodsCategories()
    }

}